import * as appConfig from '../../../app-config.js';
var app = getApp()
Page({
  data: {
    topCurrentId:'',
    leftCurrentId:'',
    leftBtn:[],
    list:[],
    goodsType:'',
    addImg:'../../../image/add.png',
    priceName:'进货价',
    retailPrice:true,
    agentId:'',
    noData:false
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
      this.getGoodsTypeList(res.data.agentId)
    })
  },
  bindLeftBtn(e){
    let type = e.currentTarget.dataset.type
    this.setData({
      leftCurrentId:e.currentTarget.dataset.id,
      goodsType:type
    })
    this.getGoodsList(type)
  },
  bindToSettlement(){
      wx.navigateTo({
        url: 'purchaseSettlement'
      })
  },
  bindToDetail(e){
    wx.navigateTo({
      url:'purchaseDetail?type=2&goodsType='+this.data.goodsType+'&id='+e.currentTarget.dataset.id+'&price='+e.currentTarget.dataset.price+'&stockPrice='+e.currentTarget.dataset.stock
    })
  },
  //获取商品列表
  getGoodsList(){
    wx.showLoading({
      title:'正在加载'
    })
    this.setData({
      list:[]
    })
    var that = this
    var data = {
      agentId:this.data.agentId,
      type:this.data.goodsType,
      pageNum:1,
      searchKey:''
    }
    let list = []
    app.request('wx_app/product/getAllProductList',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {      
        res.data.pageContent.forEach(item=>{
          list.push({
            name:item.name,
            price:item.price,
            stockPrice:item.stockPrice,
            url:item.url,
            id:item.id
          })
          that.setData({
            list
          })
        })
          wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
      if (this.data.list.length>0) {
          this.setData({
            noData:false
          })
        }else{
          this.setData({
            noData:true
          })
        }
    })
  },
  //获取商品种类
  getGoodsTypeList(agentId){
    var that = this
    var leftBtn = this.data.leftBtn
    var data = {
      agentId,
      requestType:'agent'
    }
    app.request('wx_app/mall/product/type/list',data,'GET').then(res=>{
      console.log(res)
      let btnList = res.data.product_types
      //默认种类
      that.setData({
        goodsType:res.data.product_types[0].id
      })
      btnList.forEach(item=>{
        leftBtn.push({name:item.name,id:item.id})
        that.setData({
          leftBtn
        })
      })
      that.getGoodsList()
    })
  }
})